﻿using DotNetCore.CAP.Filter;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.Cap
{
    /// <summary>
    /// A filter that surrounds execution of the subscriber.
    /// </summary>
    public class MessageSubscribeFilter: ISubscribeFilter
    {
        ILogger<MessageSubscribeFilter> _logger = null;
        public MessageSubscribeFilter(ILogger<MessageSubscribeFilter> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// Called before the subscriber executes.
        /// </summary>
        /// <param name="context">The DotNetCore.CAP.Filter.ExecutingContext</param>
        /// <returns></returns>
        public async Task OnSubscribeExecutingAsync(ExecutingContext context)
        {
            // 订阅方法执行前
            await Task.CompletedTask;
        }

        /// <summary>
        /// Called after the subscriber executes.
        /// </summary>
        /// <param name="context">The DotNetCore.CAP.Filter.ExecutingContext</param>
        /// <returns></returns>
        public async Task OnSubscribeExecutedAsync(ExecutedContext context)
        {
            // 订阅方法执行后
            await Task.CompletedTask;
        }

        /// <summary>
        /// Called after the subscriber has thrown an System.Exception.
        /// </summary>
        /// <param name="context">The DotNetCore.CAP.Filter.ExceptionContext.</param>
        /// <returns></returns>
        public async Task OnSubscribeExceptionAsync(ExceptionContext context)
        {
            // 订阅方法执行异常
            _logger.LogError(context.Exception, context.DeliverMessage.Value.ToString());
            await Task.CompletedTask;
        }

    }
}
